15-112 Fundamentals of Programming

Homework 4.2


For this homework, there is no starter file. You have to create your own .py file and submit it to Autolab. You can take a previous starter file and modify it appropriately.

  • Please add your name, Andrew id, and section at the top of the file.
  • APPLY TOP-DOWN DESIGN, USE LOTS OF HELPER FUNCTIONS.
  • This homework will be manually graded.
  • You will be graded on style. You can lose up to 10 poins for style (out of 100 points). Please see here for the style rubric.
  • You may not use recursion, or any other constructs that we have not yet covered in class.
  • You will have 3 submissions on Autolab for this homework.


  • You will be submitting multiple files for hw 4.2:
    1. snake.py: the top level function here should be run() so when run() is called, the snake animation should start.
    2. tetris.py: the top level function here should be run() so when run() is called, the tetris animation should start.

    In order to do this, you will need to put all of your files into a single folder named hw42 (note no comma between the 4 and 2), then 'zip' that folder into the file hw42.zip. You can find instructions about how to create a zip file here. Be sure to only submit a single zip file. Any other archive types (such as .rar and .tgz) will not be accepted. There is no autograder for this assignment but you will still have to submit through Autolab. And as always we will only grade your last submission.

    For any misformatting errors or wrongly named functions, we will be taking -5 points off so please make sure to follow the guidelines.

    Questions

    1. More Snake [30 pts][manually graded]

    Starting from the Snake demo code developed during lecture add the following features:
    1. A pause feature, where the game pauses when the player presses 'p' for 'pause', and where the game resumes (exactly where it left off) when the user presses 'p' again. While paused, the board should be visible, but the colors should be a bit dimmer than when unpaused.
    2. A score, which should be displayed at the top-center, above the board (which will have to be moved down by enough pixels to make room for the score). The score should start at 0 for each game, and increase by 1 each time the snake eats food.
    3. A high score list, which should be displayed as part of the "Game Over" screen at the end of each game. This is just a list of the 3 highest scores (during this execution; so if you exit and restart the program, the list is cleared), in sorted order from largest (on top) to smallest (on the bottom). If there are fewer than 3 scores available, do not list the missing scores as 0's, but just do not list them at all (thus, you will list one score after the first game).
    4. Two levels. Level 1 works just as described in the Snake tutorial. But when the snake eats the 3rd piece of food, the game switches to Level 2, with the following changes:
      1. Speed-up: make the game move noticeably faster, but not unplayably faster.
      2. Poison: in addition to randomly-placed food, a separate randomly-placed piece of red poison is placed on the board (and of course not on the snake nor on the food). If the snake eats the poison, it dies. Note that you must be sure the poison is not placed one square away from the snake's head unless that is the only location remaining. (Note that this is slightly ambiguous- when necessary, you should make design decisions, and document them!)
    5. Walls. When the game is paused, the user can click with the mouse in empty cells to create walls (which are brown), or click on walls to remove them. If the snake runs into a wall, it does not die, but rather it destroys the wall at a cost of 1 point. If that would result in a negative score, then the snake dies. At the end of a level, if any walls existed for at least 20 snake moves on that level, the score increases by 1 extra bonus point. Do not remove the walls when placing new food or poison.

    2. Tetris [70 pts][manually graded]

    Write Tetris following the step-by-step instructions here. You have to follow these instructions exactly (even if you think there might be a better way to implement a certain step.)


    Valid CSS! Valid XHTML 1.0 Strict